package ua.amsoft.brelok.manager;

import java.util.List;

import org.apache.commons.validator.routines.UrlValidator;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import android.util.Log;

/**
 * Http manager. Needed for network purposes
 * 
 * @author Tair Mustafaiev
 */
public class HttpManager {
    
    private static final String TAG = HttpManager.class.getName();
    
    /**
     * Executes GET request and returns {@link HttpResponse}
     * 
     * @param url - {@link String} url for GET
     * @param headers - {@link Header} GET request headers
     * @return {@link HttpResponse} response object
     */
    public HttpResponse doGet(final String url, final Header... headers) {
        try {
            final HttpClient httpclient = new DefaultHttpClient();
            if (UrlValidator.getInstance().isValid(url)) {
                final HttpGet httpGet = new HttpGet(url);
                addHeaders(httpGet, headers);
                Log.i(HttpManager.TAG, "HttpGET going call URL: " + url);
                // Execute HTTP Post Request
                return httpclient.execute(httpGet);
            }
        } catch (final Exception e) {
            Log.e(TAG, e.getLocalizedMessage());
        }
        return null;
    }
    
    /**
     * Executes POST request and returns {@link HttpResponse}
     * 
     * @param postURL - {@link String} url for POST
     * @param params - POST entity to be sent
     * @param headers - {@link Header} request headers
     * @return {@link HttpResponse} response object
     */
    public HttpResponse doPost(final String postURL, final List<NameValuePair> params,
            final Header... headers) {
        try {
            final HttpClient client = new DefaultHttpClient();
            if (UrlValidator.getInstance().isValid(postURL)) {
                final HttpPost httpPost = new HttpPost(postURL);
                addHeaders(httpPost, headers);
                final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
                httpPost.setEntity(entity);
                Log.i(HttpManager.TAG, "HttpPOST going call URL: " + postURL);
                return client.execute(httpPost);
            }
        } catch (final Exception e) {
            Log.e(TAG, e.getLocalizedMessage());
        }
        return null;
    }
    
    private void addHeaders(final HttpRequestBase requestBase, final Header... headers) {
        if ((headers != null) && (headers.length > 0)) {
            for (final Header header : headers) {
                requestBase.addHeader(header);
            }
        }
    }
    
}
